/*
 * UnixTermDisplay.cpp
 *
 *  Created on: Feb 10, 2012
 *      Author: hzheng
 */

#include "lcd/UnixTermDisplay.h"
#include "errorcode.h"
#include <string.h>
#include <cstddef>
namespace NS_LCD_AFW
{

CUnixTermDisplay::CUnixTermDisplay()
{
}

CUnixTermDisplay::~CUnixTermDisplay()
{
}

#ifdef ENABLE_LIB_NCURSES
#include <ncurses.h>
int CUnixTermDisplay::initialize()
{
	initscr();
	cbreak();
	nonl();
	noecho();
	start_color();
	init_pair(1, COLOR_BLUE, COLOR_GREEN);
	curs_set(false);
	intrflush(stdscr, false);
	keypad(stdscr, true);
	refresh();
	return ERR_SUCCESS;
}

int CUnixTermDisplay::unInitialize()
{
	endwin();
	return ERR_SUCCESS;
}

int CUnixTermDisplay::getColumnSize()
{
	return 32;
}

int CUnixTermDisplay::getRowSize()
{
	return 8;
}

int CUnixTermDisplay::hightlight(int x, int y, int width, int hight)
{
		for (int i = 0; i < hight; ++i)
		{
			move(y + i, x + width - 1);
			addch('@');
			move(y + i, x + width - 1);
			//chgat(width, A_REVERSE, 1, NULL);
		}
		refresh();
//	for (int i = 0; i < hight; ++i)
//	{
//		move(y + i, x);
//		chgat(width, A_REVERSE, 1, NULL);
//	}
//	refresh();
	return ERR_SUCCESS;
}

int CUnixTermDisplay::unHightlight(int x, int y, int width, int hight)
{
	for (int i = 0; i < hight; ++i)
	{
		move(y + i, x + width - 1);
		addch(' ');
		move(y + i, x + width - 1);
	}
	refresh();
	return ERR_SUCCESS;
	//return hightlight(x, y, width, hight);
}

int CUnixTermDisplay::write(int x, int y, int width, int hight,
		const char *pszString)
{
	if (NULL == pszString)
	{
		return ERR_SUCCESS;
	}
	for (int j = 0; j < hight; ++j)
	{
		for (int i = 0; i < width; ++i)
		{
			if (*pszString == '\0')
			{
				refresh();
				return ERR_SUCCESS;
			}
			move(y + j, x + i);
			addch(*pszString);
			++pszString;
		}

	}
	refresh();
	return ERR_SUCCESS;
}

int CUnixTermDisplay::writeChar(int x, int y, char c)
{
	move(y, x);
	addch(c);
	move(y, x);
	refresh();
	return ERR_SUCCESS;
}

int CUnixTermDisplay::writeLine(int x, int y, int width, const char *pszString)
{
	int len = strlen(pszString);
	if (len > width)
	{
		len = width;
	}
	for (int i = 0; i < len; ++i)
	{
		move(y, x);
		if (pszString[i] == '\r' || pszString[i] == '\n')
		{
			addch(' ');
		}
		else
		{
			addch(pszString[i]);
		}
	}
	refresh();
	return ERR_SUCCESS;
}

int CUnixTermDisplay::cleanScreen(int x, int y, int width, int hight)
{
	for (int j = 0; j < hight; ++j)
	{
		for (int i = 0; i < width; ++i)
		{
			move(y + j, x + i);
			addch(' ');
		}
	}
	refresh();
	return ERR_SUCCESS;
}

int CUnixTermDisplay::showCursor()
{
	curs_set(true);
	return ERR_SUCCESS;
}

int CUnixTermDisplay::hideCursor()
{
	curs_set(false);
	return ERR_SUCCESS;
}

int CUnixTermDisplay::getCursor(int & x, int & y)
{
	return ERR_SUCCESS;
}
int CUnixTermDisplay::setCursor(int x,int y)
{
	move(y,x);
	return ERR_SUCCESS;
}
#else
int CUnixTermDisplay::initialize()
{
	return ERR_SUCCESS;
}

int CUnixTermDisplay::unInitialize()
{
	return ERR_SUCCESS;
}

int CUnixTermDisplay::getColumnSize()
{
	return 32;
}

int CUnixTermDisplay::getRowSize()
{
	return 8;
}

int CUnixTermDisplay::hightlight(int x, int y, int width, int hight)
{
	return ERR_SUCCESS;
}

int CUnixTermDisplay::unHightlight(int x, int y, int width, int hight)
{
	return hightlight(x, y, width, hight);
}

int CUnixTermDisplay::write(int x, int y, int width, int hight,
		const char *pszString)
{
	return ERR_SUCCESS;
}

int CUnixTermDisplay::writeChar(int x, int y, char c)
{
	return ERR_SUCCESS;
}

int CUnixTermDisplay::writeLine(int x, int y, int width, const char *pszString)
{
	return ERR_SUCCESS;
}

int CUnixTermDisplay::cleanScreen(int x, int y, int width, int hight)
{
	return ERR_SUCCESS;
}

int CUnixTermDisplay::showCursor()
{
	return ERR_SUCCESS;
}

int CUnixTermDisplay::hideCursor()
{
	return ERR_SUCCESS;
}

int CUnixTermDisplay::getCursor(int & x, int & y)
{
	return ERR_SUCCESS;
}
int CUnixTermDisplay::setCursor(int x,int y)
{
	return ERR_SUCCESS;
}
#endif
} /* namespace NS_LCD_AFW */
